package mo.leetcode_top;

public class L240 {

    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        //搜索行
        for (int i = 0; i < m; i++) {
            if (matrix[i][0] > target) {
                return false;
            }
            int n0 = 0;
            int n1 = n - 1;
            while (n0 <= n1) {
                int mid = (n0 + n1) / 2;
                if (matrix[i][mid] == target) {
                    return true;
                } else if (matrix[i][mid] < target) {
                    n0 = mid + 1;
                } else {
                    n1 = mid - 1;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        L240 test = new L240();
        System.out.println(test.searchMatrix(new int[][]{{1,4,7,11,15}, {2,5,8,12,19}, {3,6,9,16,22}, {10,13,14,17,24},
                {18,21,23,26,30}}, 20));
    }

}
